<?php
	$maloaitin=$_POST['maloaitt'];
	$tieude=$_POST['tieude'];
	$tomluoc=$_POST['tomluoc'];
	$noidung=$_POST['noidung'];
	$trangthai=$_POST['trangthai'];
	$slanh=$_POST['slanh'];
	$date=date('Y-m-d');
	$layloai=$knoi->queryCsdl("select loai from loaitintuc where maloaitt='$maloaitin'");
	$l=mysql_fetch_array($layloai);
	$loai=$l['loai'];
	//$ngaydang='';
	
		//$ngaydang=$date;
	$i=1;
	/*if(isset($_FILES['anh1']))
		echo "fjsdkfjsdfs: ".$_FILES['anh'.$i]['name'];*/
	$layma=$knoi->queryCsdl("select max(matt) as max_tt from tintuc");
	$max=mysql_fetch_array($layma);
	$max_ma=$max['max_tt'];
	if($max_ma=='')
		$matt=1;
	else
		$matt=$max_ma + 1;
	echo "<br>matintuc: ".$matt;
	$query="insert into tintuc values('$matt','$tieude','$tomluoc','$noidung','','$trangthai','$maloaitin')";
	if($trangthai==1)
		$query="insert into tintuc values('$matt','$tieude','$tomluoc','$noidung',NOW(),'$trangthai','$maloaitin')";
	if($knoi->queryCsdl($query))
	{
		   $path_thumbs = "upload_images";
			//the new width of the resized image, in pixels.
			$img_thumb_width = 200; // 
	
			$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
			//List of allowed extensions if extlimit = yes
			$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
		for($i=1;$i<=$slanh;$i++)
		{
			//echo "<br>i: ".$i."<br>";
			$tam=basename( $_FILES['anh'.$i]['name']);
			//echo "fgdk".$tam;
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$result='';
			$resize='';
			//echo "<br>".$_FILES['anh'.$i]['name'];
			$duongdan ="upload_images\/".$tam;
			//the image -> variables
			$file_type = $_FILES['anh'.$i]['type'];
			$file_name = $_FILES['anh'.$i]['name'];
			$file_size = $_FILES['anh'.$i]['size'];
			$file_tmp = $_FILES['anh'.$i]['tmp_name'];
			$ext = strrchr($file_name,'.');
		   $ext = strtolower($ext);
		   /*if (($extlimit == "yes") && (!in_array($ext,$limitedext))) 
		   {
			  echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
			  exit();
		   }*/
		   //so, whats the file's extension?
		   $getExt = explode ('.', $file_name);
		   $file_ext = $getExt[count($getExt)-1];
		   $rand_name = md5(time());
		   $rand_name= rand(0,999999999);
		   $ThumbWidth = $img_thumb_width;
		   if($file_size)
		   {
			  if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
			  {
				   $new_img = imagecreatefromjpeg($file_tmp);
			   }
			   elseif($file_type == "image/x-png" || $file_type == "image/png")
			   {
				   $new_img = imagecreatefrompng($file_tmp);
			   }
			   elseif($file_type == "image/gif")
			   {
				   $new_img = imagecreatefromgif($file_tmp);
			   }
			   //list the width and height and keep the height ratio.
			   list($width, $height) = getimagesize($file_tmp);
			   //calculate the image ratio
			   $imgratio=$width/$height;
			   if ($imgratio>1)
			   {
				  $newwidth = $ThumbWidth;
				  $newheight = $ThumbWidth/$imgratio;
			   }
			   else
			   {
					 $newheight = $ThumbWidth;
					 $newwidth = $ThumbWidth*$imgratio;
			   }
			   //function for resize image.
			   if (function_exists(imagecreatetruecolor))
			   {
			   $resized_img = imagecreatetruecolor($newwidth,$newheight);
			   }
			   else
			   {
					 die("Error: Please make sure you have GD library ver 2+");
			   }
			   //the resizing is going on here!
			   imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			   //finally, save the image
			   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
			   ImageDestroy ($resized_img);
			   ImageDestroy ($new_img);
			}
			if(@move_uploaded_file($_FILES['anh'.$i]['tmp_name'], $duongdan)) 
		   {
		   		@move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");
			  $result = $duongdan;
			  $resize="$path_thumbs/$rand_name.$file_ext";
			  $knoi->queryCsdl("insert into hinhanh values('','$result','$resize','','$matt')");
		   }
		}
		echo "<script language='javascript'>alert('Đã insert tin tức thành công!');</script>";
		echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=danhsachtintuc&loai=$loai' />";
	}
	else
	{
		echo "<script language='javascript'>alert('Có lỗi trong quá trình insert!');</script>";
		echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=tintuc&tieude=".$tieude."&tomluoc=".$tomluoc."&noidung=".$noidung."'/>";
	}
	?>
	
